package com.cheerful.blog.util;

import com.alibaba.fastjson.JSON;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author: Wang Zhifu
 * @create: 2022/2/7 19:44
 * @Description:
 */
public class QiniuOssUtil {
    private static final String accessKey = "";
    private static final String secretKey = "";


    public static String upload(MultipartFile file,String bucket) throws IOException {
        Configuration cfg = new Configuration(Region.huanan());
        cfg.useHttpsDomains= false;
        UploadManager uploadManager = new UploadManager(cfg);
        byte[] bytes = file.getBytes();
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
//        开始字节数组上传到七牛云
        Response response = uploadManager.put(bytes, "blog-img/"+UUID.randomUUID().toString().replace("-",""), upToken);
        MyPutRet myPutRet = response.jsonToObject(MyPutRet.class);
        return "http://img.wzfdepress.com/"+myPutRet.key;
    }

    static class MyPutRet {
        public String key;
        public String hash;
        public String bucket;
        public long size;
    }
}
